home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Graphics Programming (2nd Edition) / Visual Basic Graphics Programming 2nd Edition.iso / Src / Ch6 / ThumbImg.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1999-04-25  |  14.1 KB  |  407 lines

  1. VERSION 5.00
  2. Begin VB.Form frmThumbImg 
  3.    Caption         =   "ThumbImg"
  4.    ClientHeight    =   5685
  5.    ClientLeft      =   1140
  6.    ClientTop       =   1800
  7.    ClientWidth     =   8715
  8.    LinkTopic       =   "Form1"
  9.    PaletteMode     =   1  'UseZOrder
  10.    ScaleHeight     =   379
  11.    ScaleMode       =   3  'Pixel
  12.    ScaleWidth      =   581
  13.    Begin VB.FileListBox filFiles 
  14.       Height          =   1065
  15.       Left            =   0
  16.       TabIndex        =   5
  17.       Top             =   1920
  18.       Width           =   2175
  19.    End
  20.    Begin VB.ComboBox cboPatterns 
  21.       Height          =   315
  22.       Left            =   0
  23.       TabIndex        =   4
  24.       Text            =   "PatternCombo"
  25.       Top             =   3240
  26.       Width           =   2175
  27.    End
  28.    Begin VB.PictureBox picHidden 
  29.       AutoSize        =   -1  'True
  30.       BorderStyle     =   0  'None
  31.       Height          =   960
  32.       Left            =   4200
  33.       ScaleHeight     =   64
  34.       ScaleMode       =   3  'Pixel
  35.       ScaleWidth      =   64
  36.       TabIndex        =   3
  37.       Top             =   480
  38.       Visible         =   0   'False
  39.       Width           =   960
  40.    End
  41.    Begin VB.PictureBox picThumb 
  42.       AutoRedraw      =   -1  'True
  43.       BorderStyle     =   0  'None
  44.       Height          =   1560
  45.       Index           =   0
  46.       Left            =   2235
  47.       ScaleHeight     =   104
  48.       ScaleMode       =   3  'Pixel
  49.       ScaleWidth      =   104
  50.       TabIndex        =   2
  51.       Top             =   0
  52.       Visible         =   0   'False
  53.       Width           =   1560
  54.    End
  55.    Begin VB.DriveListBox drvDrives 
  56.       Height          =   315
  57.       Left            =   0
  58.       TabIndex        =   1
  59.       Top             =   0
  60.       Width           =   2175
  61.    End
  62.    Begin VB.DirListBox dirDirectories 
  63.       Height          =   1155
  64.       Left            =   0
  65.       TabIndex        =   0
  66.       Top             =   360
  67.       Width           =   2175
  68.    End
  69.    Begin VB.Label lblThumb 
  70.       Alignment       =   2  'Center
  71.       BeginProperty Font 
  72.          Name            =   "Arial"
  73.          Size            =   8.25
  74.          Charset         =   0
  75.          Weight          =   400
  76.          Underline       =   0   'False
  77.          Italic          =   0   'False
  78.          Strikethrough   =   0   'False
  79.       EndProperty
  80.       Height          =   255
  81.       Index           =   0
  82.       Left            =   2235
  83.       TabIndex        =   6
  84.       Top             =   1560
  85.       Visible         =   0   'False
  86.       Width           =   1560
  87.    End
  88.    Begin VB.Menu mnuFile 
  89.       Caption         =   "&File"
  90.       Begin VB.Menu mnuFileExit 
  91.          Caption         =   "E&xit"
  92.       End
  93.    End
  94.    Begin VB.Menu mnuThumbs 
  95.       Caption         =   "&Thumbs"
  96.       Begin VB.Menu mnuThumbsShow 
  97.          Caption         =   "&Show"
  98.          Shortcut        =   {F5}
  99.       End
  100.       Begin VB.Menu mnuThumbsSize 
  101.          Caption         =   "S&ize"
  102.          Begin VB.Menu mnuThumbsSetSize 
  103.             Caption         =   "&Small"
  104.             Index           =   50
  105.             Shortcut        =   ^S
  106.          End
  107.          Begin VB.Menu mnuThumbsSetSize 
  108.             Caption         =   "&Medium"
  109.             Index           =   100
  110.             Shortcut        =   ^M
  111.          End
  112.          Begin VB.Menu mnuThumbsSetSize 
  113.             Caption         =   "&Large"
  114.             Index           =   200
  115.             Shortcut        =   ^L
  116.          End
  117.       End
  118.    End
  119. Attribute VB_Name = "frmThumbImg"
  120. Attribute VB_GlobalNameSpace = False
  121. Attribute VB_Creatable = False
  122. Attribute VB_PredeclaredId = True
  123. Attribute VB_Exposed = False
  124. Option Explicit
  125. Private Running As Boolean
  126. Private DirName As String
  127. Private MaxFileNum As Integer
  128. Private SelectedThumb As Integer
  129. Private ThumbSize As Single
  130. ' API stuff for moving files to the wastebasket.
  131. Private Type SHFILEOPSTRUCT
  132.     hwnd As Long
  133.     wFunc As Long
  134.     pFrom As String
  135.     pTo As String
  136.     fFlags As Integer
  137.     fAnyOperationsAborted As Long
  138.     hNameMappings As Long
  139.     lpszProgressTitle As Long '  only used if FOF_SIMPLEPROGRESS
  140. End Type
  141. Private Declare Function SHFileOperation Lib "shell32.dll" Alias "SHFileOperationA" (lpFileOp As SHFILEOPSTRUCT) As Long
  142. Private Const FO_DELETE = &H3
  143. Private Const FOF_ALLOWUNDO = &H40
  144. Private Const FOF_NOCONFIRMATION = &H10
  145. ' API stuff for LoadImage.
  146. Private Declare Function SelectObject Lib "gdi32" (ByVal hdc As Long, ByVal hObject As Long) As Long
  147. Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
  148. Private Declare Function LoadImage Lib "user32" Alias "LoadImageA" (ByVal hInst As Long, ByVal lpsz As String, ByVal un1 As Long, ByVal n1 As Long, ByVal n2 As Long, ByVal un2 As Long) As Long
  149. Private Const LR_LOADFROMFILE = &H10&
  150. Private Const IMAGE_BITMAP = 0
  151. Private Const IMAGE_ICON = 1
  152. Private Const IMAGE_CURSOR = 2
  153. ' Load a bitmap file into a PictureBox using
  154. ' LoadImage.
  155. Private Sub LoadImageFile(ByVal pic As PictureBox, ByVal file_name As String)
  156. Dim wid As Long
  157. Dim hgt As Long
  158. Dim hbmp As Long
  159. Dim image_hdc As Long
  160.     ' Get the PictureBox's dimensions in pixels.
  161.     wid = pic.ScaleX(pic.ScaleWidth, pic.ScaleMode, vbPixels)
  162.     hgt = pic.ScaleY(pic.ScaleHeight, pic.ScaleMode, vbPixels)
  163.     ' Load the bitmap.
  164.     hbmp = LoadImage(0, file_name, IMAGE_BITMAP, _
  165.         wid, hgt, LR_LOADFROMFILE)
  166.     ' Make the picture box display the image.
  167.     SelectObject pic.hdc, hbmp
  168.     ' Destroy the bitmap to free its resources.
  169.     DeleteObject hbmp
  170.     ' Refresh the image.
  171.     pic.Refresh
  172. End Sub
  173. ' Move the file into the wastebasket.
  174. Private Sub DeleteFile(ByVal Index As Integer)
  175. Dim op As SHFILEOPSTRUCT
  176. Dim file_name As String
  177.     file_name = DirName & lblThumb(Index).Caption
  178.     file_name = DirName & lblThumb(Index).Caption
  179.     With op
  180.         .wFunc = FO_DELETE
  181.         .pFrom = file_name
  182.         .fFlags = FOF_ALLOWUNDO Or FOF_NOCONFIRMATION
  183.     End With
  184.     SHFileOperation op
  185.     If Not op.fAnyOperationsAborted Then
  186.         ' Mark the file as deleted.
  187.         lblThumb(Index).Caption = ""
  188.         picThumb(Index).Line (0, 0)- _
  189.             (picThumb(Index).ScaleWidth, _
  190.              picThumb(Index).ScaleHeight)
  191.         picThumb(Index).Line _
  192.             (picThumb(Index).ScaleWidth, 0)- _
  193.             (0, picThumb(Index).ScaleHeight)
  194.     End If
  195. End Sub
  196. ' Display thumbnails for this directory.
  197. Private Sub ShowThumbs()
  198. Const GAP = 2
  199. Dim i As Integer
  200. Dim new_name As String
  201. Dim new_ext As String
  202. Dim wid As Single
  203. Dim hgt As Single
  204. Dim thumb_left As Single
  205. Dim thumb_top As Single
  206.     MaxFileNum = 0
  207.     SelectedThumb = -1
  208.     ' Get the directory name.
  209.     DirName = dirDirectories.Path
  210.     If Right$(DirName, 1) <> "\" Then
  211.         DirName = DirName & "\"
  212.     End If
  213.     ' Hide the thumbnail pictures.
  214.     For i = 0 To picThumb.UBound
  215.         picThumb(i).Visible = False
  216.         lblThumb(i).Visible = False
  217.     Next i
  218.     ' See where the first thumb goes.
  219.     thumb_left = drvDrives.Left + drvDrives.Width + GAP
  220.     thumb_top = 0
  221.     ' Get the file names.
  222.     For i = 0 To filFiles.ListCount - 1
  223.         new_name = filFiles.List(i)
  224.         new_ext = LCase$(Right$(new_name, 3))
  225.         ' Load the file.
  226.         On Error Resume Next
  227.         ' Load the picture using LoadPicture.
  228.         picHidden.Picture = LoadPicture(DirName & new_name)
  229.         If Err.Number = 0 Then
  230.             ' We loaded the picture successfully.
  231.             ' Display its thumbnail.
  232.             On Error GoTo 0
  233.             ' Calculate the thumbnail size.
  234.             wid = picHidden.ScaleWidth
  235.             hgt = picHidden.ScaleHeight
  236.             If wid > ThumbSize Then
  237.                 hgt = hgt * ThumbSize / wid
  238.                 wid = ThumbSize
  239.             End If
  240.             If hgt > ThumbSize Then
  241.                 wid = wid * ThumbSize / hgt
  242.                 hgt = ThumbSize
  243.             End If
  244.             ' Load the thumbnail picture.
  245.             If MaxFileNum > picThumb.UBound Then
  246.                 Load picThumb(MaxFileNum)
  247.                 Load lblThumb(MaxFileNum)
  248.             End If
  249.             ' Display the thumbnail.
  250.             picThumb(MaxFileNum).BorderStyle = vbBSNone
  251.             
  252.             ' See if this is a bitmap.
  253.             If (new_ext = "bmp") Then
  254.                 ' Load the picture using LoadImage.
  255.                 ' Make the thumbnail the right shape.
  256.                 picThumb(MaxFileNum).Move _
  257.                     thumb_left + (ThumbSize - wid) / 2, _
  258.                     thumb_top + (ThumbSize - hgt) / 2, _
  259.                     wid, hgt
  260.                 picThumb(MaxFileNum).Picture = picThumb(MaxFileNum).Image
  261.                 ' Display the image.
  262.                 LoadImageFile picThumb(MaxFileNum), DirName & new_name
  263.             Else
  264.                 ' Copy the picture using PaintPicture.
  265.                 ' Make the thumbnail fill its area.
  266.                 picThumb(MaxFileNum).Move _
  267.                     thumb_left, thumb_top, _
  268.                     ThumbSize, ThumbSize
  269.                 ' Clear the thumbnail.
  270.                 picThumb(MaxFileNum).Line (0, 0)-(picThumb(MaxFileNum).ScaleWidth, picThumb(MaxFileNum).ScaleHeight), vbWhite, BF
  271.                 ' Copy the image reduced.
  272.                 picThumb(MaxFileNum).PaintPicture _
  273.                     picHidden.Picture, _
  274.                     (ThumbSize - wid) / 2, _
  275.                     (ThumbSize - hgt) / 2, wid, hgt, _
  276.                     0, 0, picHidden.ScaleWidth, picHidden.ScaleHeight
  277.             End If
  278.             picThumb(MaxFileNum).Visible = True
  279.             lblThumb(MaxFileNum).Move _
  280.                 thumb_left, thumb_top + ThumbSize, _
  281.                 ThumbSize
  282.             lblThumb(MaxFileNum).Caption = new_name
  283.             lblThumb(MaxFileNum).Visible = True
  284.             MaxFileNum = MaxFileNum + 1
  285.             ' See where the next thumb goes.
  286.             thumb_left = thumb_left + ThumbSize + GAP
  287.             If thumb_left + ThumbSize > ScaleWidth Then
  288.                 thumb_left = drvDrives.Left + drvDrives.Width + GAP
  289.                 thumb_top = thumb_top + ThumbSize + _
  290.                     lblThumb(0).Height + 3 * GAP
  291.                 If thumb_top + ThumbSize > ScaleHeight Then Exit For
  292.             End If
  293.             DoEvents
  294.             If Not Running Then Exit Sub
  295.         End If ' End if we got no error loading the picture.
  296.     Next i
  297. End Sub
  298. ' The user selected a directory. Let the filFiles
  299. ' control know so it can update its list.
  300. Private Sub dirDirectories_Change()
  301.     filFiles.Path = dirDirectories.Path
  302. End Sub
  303. ' The user selected a drive. Let the dirDirectories
  304. ' control know so it can update its list.
  305. Private Sub drvDrives_Change()
  306.     'On Error GoTo DriveError
  307.     dirDirectories.Path = drvDrives.Drive
  308.     Exit Sub
  309. DriveError:
  310.     drvDrives.Drive = dirDirectories.Path
  311.     Exit Sub
  312. End Sub
  313. ' Create the list of file patterns.
  314. Private Sub Form_Load()
  315.     dirDirectories.Path = App.Path
  316.     cboPatterns.AddItem "Bitmaps (*.bmp)"
  317.     cboPatterns.AddItem "GIFs (*.gif)"
  318.     cboPatterns.AddItem "JPEGs (*.jpg)"
  319.     cboPatterns.AddItem "Icons (*.ico)"
  320.     cboPatterns.AddItem "Cursors (*.cur)"
  321.     cboPatterns.AddItem "Run-Length Encoded (*.rle)"
  322.     cboPatterns.AddItem "Metafiles (*.wmf)"
  323.     cboPatterns.AddItem "Enhanced Metafiles (*.emf)"
  324.     cboPatterns.AddItem "Graphic Files (*.bmp;*.gif;*.jpg;*.jpeg;*.ico;*.cur;*.rle;*.wmf;*.emf)"
  325.     cboPatterns.AddItem "All Files (*.*)"
  326.     cboPatterns.ListIndex = 8
  327.     mnuThumbsSetSize_Click 100
  328. End Sub
  329. ' Make the controls fill the form.
  330. Private Sub Form_Resize()
  331. Const GAP = 2
  332. Dim wid As Integer
  333. Dim hgt As Integer
  334.     If WindowState = vbMinimized Then Exit Sub
  335.     wid = drvDrives.Width
  336.     drvDrives.Move GAP, GAP, wid
  337.     cboPatterns.Move GAP, ScaleHeight - cboPatterns.Height, wid
  338.     hgt = (cboPatterns.Top - drvDrives.Top - drvDrives.Height - 3 * GAP) / 2
  339.     If hgt < 100 Then hgt = 100
  340.     dirDirectories.Move GAP, drvDrives.Top + drvDrives.Height + GAP, wid, hgt
  341.     filFiles.Move GAP, dirDirectories.Top + dirDirectories.Height + GAP, wid, hgt
  342. End Sub
  343. Private Sub mnuFileExit_Click()
  344.     Unload Me
  345. End Sub
  346. ' Set the thumbnail size.
  347. Private Sub mnuThumbsSetSize_Click(Index As Integer)
  348.     mnuThumbsSetSize(50).Checked = False
  349.     mnuThumbsSetSize(100).Checked = False
  350.     mnuThumbsSetSize(200).Checked = False
  351.     mnuThumbsSetSize(Index).Checked = True
  352.     ThumbSize = Index
  353.     mnuThumbsShow_Click
  354. End Sub
  355. ' Start or stop displaying thumbnails.
  356. Private Sub mnuThumbsShow_Click()
  357.     If Running Then
  358.         ' Stop.
  359.         mnuThumbsShow.Enabled = False
  360.         mnuThumbsShow.Caption = "Stopping"
  361.         Running = False
  362.         DoEvents
  363.     Else
  364.         ' Start.
  365.         mnuThumbsShow.Caption = "Stop"
  366.         Running = True
  367.         MousePointer = vbHourglass
  368.         DoEvents
  369.         ShowThumbs
  370.         Running = False
  371.         mnuThumbsShow.Caption = "Show"
  372.         mnuThumbsShow.Enabled = True
  373.         MousePointer = vbDefault
  374.     End If
  375. End Sub
  376. ' The user selected a pattern. Let the filFiles
  377. ' control know so it can filter its list.
  378. Private Sub cboPatterns_Click()
  379. Dim pat As String
  380. Dim p1 As Integer
  381. Dim p2 As Integer
  382.     pat = cboPatterns.List(cboPatterns.ListIndex)
  383.     p1 = InStr(pat, "(")
  384.     p2 = InStr(pat, ")")
  385.     filFiles.Pattern = Mid$(pat, p1 + 1, p2 - p1 - 1)
  386. End Sub
  387. ' The user clicked on a thumbnail. Select it.
  388. Private Sub picThumb_Click(Index As Integer)
  389.     If SelectedThumb >= 0 Then
  390.         picThumb(SelectedThumb).BorderStyle = vbBSNone
  391.     End If
  392.     SelectedThumb = Index
  393.     picThumb(SelectedThumb).BorderStyle = vbFixedSingle
  394.     Caption = "Thumbs - " & lblThumb(SelectedThumb).Caption
  395. End Sub
  396. ' The user pressed a key while a thumbnail had
  397. ' the focus. If it is the delete key, move the
  398. ' file into the waste basket.
  399. Private Sub picThumb_KeyDown(Index As Integer, KeyCode As Integer, Shift As Integer)
  400.     If (KeyCode = vbKeyDelete) And _
  401.        (Len(lblThumb(Index).Caption) > 0) _
  402.     Then
  403.         ' Move the file into the wastebasket.
  404.         DeleteFile Index
  405.     End If
  406. End Sub
  407.